#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>

#include "core.h"
#include "guess_number.h"
#include "user_operation.h"

static int run_guess_game(struct guess* core_guess_data);

void guess_number(struct guess* core_guess_data)
{
	if (!core_guess_data) {
        DEBUG("PARAM ERROR!\n");
        return;
    }
    
    init_guess_game(core_guess_data);
    
    run_guess_game(core_guess_data);
}

int run_guess_game(struct guess* core_guess_data)
{
	if (!core_guess_data) {
        DEBUG("PARAM ERROR!\n");
        return 0;
    }
    
    system("clear");
    printf("\t\t------------guess number-------------\n");
    printf("\t\t游戏规则：猜测并输入0到100中的整数，输入回车为确认，输入999为退出游戏\n");
    printf("\t\tWelcome to guessing digital games!\n"); 
    printf("\t\tPlease enter your guess number:");
    
	int guess_number = 111;
    while (core_guess_data->right_number != guess_number) {
    	guess_number = judge_number(guess_number);
    		
	    if (guess_number == 999) {
            break;
	    }

	    if (core_guess_data->right_number > core_guess_data->small_number && core_guess_data->right_number < guess_number) {
            if (guess_number < core_guess_data->biggest_number) {
		    	core_guess_data->biggest_number = guess_number;
			}
			printf("\n\t\tNumber error, please enter again\n");
			printf("\t\tThe numbers you entered are too large. Please enter numbers between %d and %d:",
			core_guess_data->small_number, core_guess_data->biggest_number);
	    }
		else if (core_guess_data->right_number > guess_number && core_guess_data->right_number < core_guess_data->biggest_number) {
			if (guess_number > core_guess_data->small_number) {
		    	core_guess_data->small_number = guess_number;
			}
			printf("\n\t\tNumber error, please enter again\n");
			printf("\t\tThe numbers you entered are too small. Please enter numbers between %d and %d:",
			core_guess_data->small_number, core_guess_data->biggest_number);
	    }
	}
    if (guess_number == 999) {
        return 0;
    }

	printf("\n\t\tCongratulations, you guessed it!\n");
	sleep(2);
	
	return 0;
}

/*
 *@brief        随机生成一个100以内的数字
 */
int get_random(void)
{	
	int right_number;

    srand(time(NULL));
    right_number = rand()%100+1;

    return right_number;
}
